package com.lianjia.sh.se.message.event.client.publisher.impl;

import com.alibaba.fastjson.JSONObject;
import com.lianjia.sh.se.message.event.client.model.MessageEvent;
import com.lianjia.sh.se.message.event.client.publisher.kafka.MessageEventKafkaSender;
import com.lianjia.sh.se.message.event.client.publisher.kafka.MessageEventKafkaSenderFactory;
import com.lianjia.sh.se.message.event.client.publisher.MessageEventPublisher;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * Created by yangsp1 on 16/8/3.
 */
@Component
public class MessageEventPublisherImpl implements MessageEventPublisher {
    @Autowired
    MessageEventKafkaSenderFactory messageEventKafkaSenderFactory;

    /**
     * 发布事件
     * @param event 通过消息传递的事件
     */
    public void publishEvent(MessageEvent event){
        String projectCode = event.getProjectCode();
        String serviceCode = event.getServiceCode();
        Assert.isTrue(StringUtils.isNotBlank(projectCode),"项目编码不能为空");
        Assert.isTrue(StringUtils.isNotBlank(serviceCode),"服务编码不能为空");
        MessageEventKafkaSender kafkaSender = messageEventKafkaSenderFactory.createKafkaSender(projectCode,serviceCode);
        String msg = JSONObject.toJSONString(event);
        kafkaSender.sendAsyn(msg);
    }
}
